<script lang="ts" setup>
import { useVbenModal } from '@vben/common-ui';
import { useVbenForm } from '#/adapter/form';
import { addMaintenance } from '#/api/gis/maintenance';
import type { AddMaintenanceParams } from '#/api/gis/maintenance';
import { message } from 'ant-design-vue';


const [AddModal, AddmodalApi] = useVbenModal({
});

const [BaseForm] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
  },
  handleSubmit: onSubmit,
  layout: 'horizontal',
  schema: [
  {
      component: 'Select',
      componentProps: {
        allowClear: true,
        filterOption: true,
        options: [
          {
            label: '油水井',
            value: '0',
          },
          {
            label: '计量间',
            value: '1',
          },
          {
            label: '转油站',
            value: '2',
          },
          {
            label: '管线',
            value: '3',
          },
          {
            label: '阀池',
            value: '4',
          },
        ],
        placeholder: '请选择维修对象类型',
        showSearch: true,
      },
      fieldName: 'type',
      label: '维修对象类型',
      rules: 'selectRequired',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入维修对象编号',
      },
      fieldName: 'featureCode',
      label: '维修对象编号：',
      rules: "required"
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '1级',
            value: 1,
          },
          {
            label: '2级',
            value: 2,
          },
          {
            label: '3级',
            value: 3,
          },
          {
            label: '4级',
            value: 4,
          },
          {
            label: '5级',
            value: 5,
          },
        ],
        placeholder: '请选择维修等级',
      },
      fieldName: 'ranking',
      label: '维修等级：',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入备注',
      },
      fieldName: 'remark',
      label: '备注',
      rules: "required"
    },
  ],
});

// 定义一个自定义事件
const emit = defineEmits(['refreshTable']);
function onSubmit(values: Record<string, any>) {
  const params = values as AddMaintenanceParams;
  addMaintenance(params).then(() => {
    AddmodalApi.close();
    message.success('添加维修记录成功');
    emit('refreshTable'); // 触发自定义事件
  }).catch((error) => {
    console.error('添加维修记录失败:', error);
  });
}
</script>

<template>
  <AddModal title="添加维修记录">
    <BaseForm />
    <template #prepend-footer>
    </template>
  </AddModal>
</template>
